package cn.xsshome.algorithmNation.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.xsshome.algorithmNation.model.AntCipherRO;
import cn.xsshome.algorithmNation.model.Keys;
import cn.xsshome.algorithmNation.vo.AntRO;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.InvalidKeySpecException;

/**
 * Create by heshun on 2018/11/26
 */
public class AntROParser {

    public static void main(String[] args) throws NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException, InvalidKeyException, InvalidKeySpecException {
        AntRO data = getData(AntCipherRO.getRo(), AntRO.class);
//        System.out.println(data.getId());
//        testVerify();
    }

    public static <T> T getData(AntCipherRO ro, Class<?> clazz) throws NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException, InvalidKeyException, InvalidKeySpecException {
        String sign = ro.getSign();
//        try {
//            //验证签名
//            boolean verify = SecureUtil.sign(SHA256withRSA, null, Keys.ALI_PUBLICK_KEY)
//                    .verify(Base64.decode(ro.getSignData()), Base64.decode(ro.getSign()));
//            if (verify) {
//                System.out.println("verify" + SHA256withRSA);
//            }else {
//                System.out.println("verify false");
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        RSAPrivateKey pk = RsaUtils.INSTANCE.loadPKCS8PriKeyFromBase64(Keys.CENTNET_PRIVATE_KEY);
        String s = RsaUtils.INSTANCE.decryptBase64String(pk, ro.getKeyData());
        System.out.println(s);

//        RSA rsa = SecureUtil.rsa(Keys.CENTNET_PRIVATE_KEY, Keys.CENTNET_PUBLICK_KEY);
//        byte[] decrypt = rsa.decryptFromBase64(ro.getKeyData(), KeyType.PrivateKey);
//        System.out.println(StrUtil.str(decrypt, CharsetUtil.UTF_8));
//        byte[] bytes = SM4EnDecryption.encrypt_Ecb_Padding(decrypt, Base64.decode(ro.getData()));
//        System.out.println(StrUtil.str(bytes, CharsetUtil.UTF_8));
        return null;
    }

    public static void testVerify() {
        System.out.println(AntCipherRO.getRo().getSignData());

//        Sign sign = SecureUtil.sign(SHA256withRSA, Keys.CENTNET_PRIVATE_KEY, Keys.CENTNET_PUBLICK_KEY);
//        AntCipherRO ro = AntCipherRO.getRo();
//        byte[] sign2 = sign.sign(ro.getSignData().getBytes());
////        System.out.println(sign2.length);
//        System.out.println(ro.getSign().length());


//        String s = "123123213";
//        byte[] sign1 = sign.sign(s.getBytes());
//        sign.verify(s.getBytes(), sign1);
    }
}
